import Vue from 'vue';
import Vuex from 'vuex';
import {
	http
} from '../common/api/index';

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		userInfo: null, // 用户信息
		tabDynamics: [] // 获取点击之后的tab标签
	},
	mutations: {
		// 初始化APP,获取缓存的token,用户信息等其他缓存内容
		initApp(state) {
			const _userInfo = uni.getStorageSync('USER_INFO');
			if (_userInfo) {
				state.userInfo = _userInfo;
				global.userInfo = _userInfo;
				global.token = _userInfo.token;
			}
		},
		// 更新用户信息
		updateUserInfo(state, data) {
			if (state.userInfo) {
				state.userInfo = {
					...state.userInfo,
					...data,
				};
			} else {
				state.userInfo = data;
			}
			uni.setStorageSync('USER_INFO', state.userInfo);
			global.userInfo = state.userInfo;
			if (state.userInfo.token) {
				global.token = state.userInfo.token;
			}
		},
		// 退出登录
		logout(state) {
			state.userInfo = null;
			global.userInfo = null;
			global.token = null;
			uni.removeStorageSync('USER_INFO');
		},
		updateTabDynamics(state, list) {
			state.tabDynamics = list
		}
	},
	getters: {
		// 用户Token
		token(state) {
			if (state.userInfo && state.userInfo.token) {
				return state.userInfo.token;
			} else {
				return null;
			}
		},
		// 用户信息
		userInfo(state) {
			if (state.userInfo) {
				return state.userInfo;
			} else {
				return null;
			}
		},
		// 获取tabDynamics
		tabDynamics(state) {
			return state.tabDynamics;
		},

	},
	actions: {

	}
})
export default store;